package com.example.fileserver.controller;

import com.example.fileserver.entity.R;
import com.example.fileserver.entity.ResConst;
import com.example.fileserver.client.minio.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author yousj
 * @date 2021/8/1
 */
@RestController
@RequestMapping("/minio")
public class MinioFileController {

    @Autowired
    MinioUtil minioUtil;

    @PostMapping("/upload")
    public R upload(MultipartFile file, @RequestParam(value = "bucketName") String bucketName){
        if (!minioUtil.upload(file, bucketName)) {
            return R.isFail(ResConst.SYSTEM_ERROR);
        }
        return R.isOk();
    }

    @GetMapping("/download")
    public void download(@RequestParam(value = "bucketName") String bucketName,
                      @RequestParam(value = "fileName") String fileName, HttpServletResponse response){
        minioUtil.download(bucketName,fileName,response);
    }

}
